package com.ylw.dao;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

/**
 * Created by yangliwei on 2017/7/12.
 */
public class DXGameRankDao {
    SessionFactory sessionFactory = null;
    @Before
    public void before(){
        sessionFactory = new Configuration().configure().buildSessionFactory();
    }

    @SuppressWarnings("all")
    @Test
    public void test(){
        Session session = sessionFactory.openSession();
        String sql = "select rownum from (select * ,@rownum \\:= @rownum + 1 as 'rownum' from dx_game_rank ,(select @rownum \\:=0) b where gameId = ? order by highestScore desc) T where T.userId =?";
        SQLQuery sqlQuery = session.createSQLQuery(sql).setParameter(0,2).setParameter(1,1);
        List list = sqlQuery.list();
        if(list.size()>0){
            Integer result = (Integer) list.get(0);
        }
        int max = sqlQuery.getMaxResults();
        int first  = sqlQuery.getFirstResult();
        session.close();
    }
    @SuppressWarnings("all")
    @Test
    public void test1(){
        Session session = sessionFactory.openSession();
        String sql = "select rownum from (select * ,@rownum := @rownum + 1 as 'rownum' where game_id = ? order by highest_score desc) T where t.id =? ";
        Query query = session.createQuery(sql).setParameter(0,1).setParameter(1,1);
        query.list().get(0);
        session.close();
    }
}
